<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="title" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="养殖类型" prop="breedType">
                <el-select class="w100p" v-model="ruleForm.breedType" clearable filterable multiple placeholder="请选择">
                    <el-option v-for="item in breedTypeList" :key="item.breedType" :label="item.name"
                        :value="item.breedType">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 认</el-button>
        </span>
    </el-dialog>
</template>
<script>
import unitManagement from '@/api/basicInformation/unitManagement';
export default {
    components: {},
    props: {
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        list: {
            type: Array,
            default: () => {
                return []
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        }
    },
    data() {
        return {
            breedTypeList: [
                {
                    name: "吊水养殖",
                    breedType: "HANGING_WATER_BREED"
                },
                {
                    name: "传统养殖",
                    breedType: "TRADITION_BREED"
                },
                {
                    name: "循环水养殖",
                    breedType: "CIRCULATE_BREED"
                },
                {
                    name: "跑道养殖",
                    breedType: "RUNWAY_BREED"
                }
            ],
            ruleForm: {
                breedType: []
            },
            rules: {
                breedType: [
                    { required: true, message: '请选择养殖类型', trigger: 'change' }
                ]
            }
        }
    },
    watch: {
        dialogVisible(v) {
            v && this.resetForm();
            if (this.list.length == 0 && v) {
                this.$message.warning('请先选择公共库内的数据再进行添加');
                this.cancel();
            }
        }
    },
    
    mounted() { },
    methods: {
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    let breedType = this.ruleForm.breedType.join(',')
                    unitManagement.importPublicList(breedType, this.list).then(res => {
                        this.$message[res.code == 1 ? 'success' : 'warning'](res.code == 1 ? '操作成功' : res.msg || '操作失败');
                        res.code == 1 && this.cancel();
                    }).catch(err => {
                        this.$message.error(err);
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$set(this.ruleForm, 'breedType', [])
            setTimeout(() => {
                this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
            }, 20)
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped></style>
  